package com.estar.worktime.service.impl;

import org.jeecgframework.core.common.service.impl.CommonServiceImpl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.estar.worktime.entity.TmblWorktimeEntity;
import com.estar.worktime.service.TmblWorktimeServiceI;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.io.Serializable;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.core.util.MyClassLoader;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.cgform.enhance.CgformEnhanceJavaInter;

@Service("tmblWorktimeService")
@Transactional
public class TmblWorktimeServiceImpl extends CommonServiceImpl implements TmblWorktimeServiceI {

	
 	public void delete(TmblWorktimeEntity entity) throws Exception{
 		super.delete(entity);
 		//执行删除操作增强业务
		this.doDelBus(entity);
 	}
 	
 	public Serializable save(TmblWorktimeEntity entity) throws Exception{
 		Serializable t = super.save(entity);
 		//执行新增操作增强业务
 		this.doAddBus(entity);
 		return t;
 	}
 	
 	public void saveOrUpdate(TmblWorktimeEntity entity) throws Exception{
 		super.saveOrUpdate(entity);
 		//执行更新操作增强业务
 		this.doUpdateBus(entity);
 	}
 	
 	/**
	 * 新增操作增强业务
	 * @param t
	 * @return
	 */
	private void doAddBus(TmblWorktimeEntity t) throws Exception{
 	}
 	/**
	 * 更新操作增强业务
	 * @param t
	 * @return
	 */
	private void doUpdateBus(TmblWorktimeEntity t) throws Exception{
 	}
 	/**
	 * 删除操作增强业务
	 * @param id
	 * @return
	 */
	private void doDelBus(TmblWorktimeEntity t) throws Exception{
 	}
 	
 	private Map<String,Object> populationMap(TmblWorktimeEntity t){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("id", t.getId());
		map.put("empcdoe", t.getEmpcdoe());
		map.put("empname", t.getEmpname());
		map.put("workplace", t.getWorkplace());
		map.put("workdate", t.getWorkdate());
		map.put("rest", t.getRest());
		map.put("worktime", t.getWorktime());
		map.put("ondutytime", t.getOndutytime());
		map.put("lateflage", t.getLateflage());
		map.put("latetime", t.getLatetime());
		map.put("seriouslateflage", t.getSeriouslateflage());
		map.put("seriouslatetime", t.getSeriouslatetime());
		map.put("offdutytime", t.getOffdutytime());
		map.put("leaveearlyflage", t.getLeaveearlyflage());
		map.put("leaveearlytime", t.getLeaveearlytime());
		map.put("seriousleaveflage", t.getSeriousleaveflage());
		map.put("seriousleavetime", t.getSeriousleavetime());
		map.put("ondutycard", t.getOndutycard());
		map.put("offdutycard", t.getOffdutycard());
		map.put("nocar", t.getNocar());
		map.put("work", t.getWork());
		map.put("nowork", t.getNowork());
		return map;
	}
 	
 	/**
	 * 替换sql中的变量
	 * @param sql
	 * @param t
	 * @return
	 */
 	public String replaceVal(String sql,TmblWorktimeEntity t){
 		sql  = sql.replace("#{id}",String.valueOf(t.getId()));
 		sql  = sql.replace("#{empcdoe}",String.valueOf(t.getEmpcdoe()));
 		sql  = sql.replace("#{empname}",String.valueOf(t.getEmpname()));
 		sql  = sql.replace("#{workplace}",String.valueOf(t.getWorkplace()));
 		sql  = sql.replace("#{workdate}",String.valueOf(t.getWorkdate()));
 		sql  = sql.replace("#{rest}",String.valueOf(t.getRest()));
 		sql  = sql.replace("#{worktime}",String.valueOf(t.getWorktime()));
 		sql  = sql.replace("#{ondutytime}",String.valueOf(t.getOndutytime()));
 		sql  = sql.replace("#{lateflage}",String.valueOf(t.getLateflage()));
 		sql  = sql.replace("#{latetime}",String.valueOf(t.getLatetime()));
 		sql  = sql.replace("#{seriouslateflage}",String.valueOf(t.getSeriouslateflage()));
 		sql  = sql.replace("#{seriouslatetime}",String.valueOf(t.getSeriouslatetime()));
 		sql  = sql.replace("#{offdutytime}",String.valueOf(t.getOffdutytime()));
 		sql  = sql.replace("#{leaveearlyflage}",String.valueOf(t.getLeaveearlyflage()));
 		sql  = sql.replace("#{leaveearlytime}",String.valueOf(t.getLeaveearlytime()));
 		sql  = sql.replace("#{seriousleaveflage}",String.valueOf(t.getSeriousleaveflage()));
 		sql  = sql.replace("#{seriousleavetime}",String.valueOf(t.getSeriousleavetime()));
 		sql  = sql.replace("#{ondutycard}",String.valueOf(t.getOndutycard()));
 		sql  = sql.replace("#{offdutycard}",String.valueOf(t.getOffdutycard()));
 		sql  = sql.replace("#{nocar}",String.valueOf(t.getNocar()));
 		sql  = sql.replace("#{work}",String.valueOf(t.getWork()));
 		sql  = sql.replace("#{nowork}",String.valueOf(t.getNowork()));
 		sql  = sql.replace("#{UUID}",UUID.randomUUID().toString());
 		return sql;
 	}
 	
 	/**
	 * 执行JAVA增强
	 */
 	private void executeJavaExtend(String cgJavaType,String cgJavaValue,Map<String,Object> data) throws Exception {
 		if(StringUtil.isNotEmpty(cgJavaValue)){
			Object obj = null;
			try {
				if("class".equals(cgJavaType)){
					//因新增时已经校验了实例化是否可以成功，所以这块就不需要再做一次判断
					obj = MyClassLoader.getClassByScn(cgJavaValue).newInstance();
				}else if("spring".equals(cgJavaType)){
					obj = ApplicationContextUtil.getContext().getBean(cgJavaValue);
				}
				if(obj instanceof CgformEnhanceJavaInter){
					CgformEnhanceJavaInter javaInter = (CgformEnhanceJavaInter) obj;
					javaInter.execute(data);
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception("执行JAVA增强出现异常！");
			} 
		}
 	}
}